= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
 
 A u t h o r :   Y o u n g w o o k   K i m   ( K o r e a n ) 
 
 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
 
 
 
 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
 
 C o n t a c t :   r u m i a 0 6 0 1 @ g m a i l . c o m 
 
 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
 
 
 
 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
 
 a n d   p l u s   a l p h a 
 
 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
 
 
 
 A c t u a l l y ,   e v e r y t h i n g   d o e s n  t   s e e m   l i k e   a   g a m e .   N o w ,   w e   w i l l   i n s e r t   a   r u l e   i n t o   t h i s   p r o g r a m .   T h e n .   I t   w i l l   b e c o m e   g a m e .   R u l e   i s   s i m p l e :   c o u n t i n g   r e d   o r   b l a c k   f r o m   5 x 5   2 D   a r r a y   a n d   c h o o s e   t h e   c o l o r   w h i c h   h a s   m u c h   m o r e   n u m b e r !   I f   c o r r e c t ,   H P + + ,   o t h e r w i s e ,   H P - - .   T h e n   n e w   a r r a y   w i l l   b e   s e t   f o r   n e x t   q u i z !   t o o   s i m p l e   b u t   g a m e   w h i c h   c a n   b e   m a d e   i n   t h i s   t u t o r i a l .   F i r s t ,   w e   n e e d   t o   g e n e r a t e   2 D   a r r a y   a n d   p r i n t   i t .   H o w ?   W e   l e a r n e d   h o w   t o   p r i n t   i n t e g e r   d a t a   ( w h i c h   e q u a l s   s i n g l e   d a t a   ( 0 D   a r r a y ) )   a n d   t w o   b u t t o n s   ( w h i c h   e q u a l s   s i n g l e   a r r a y   ( 1 D   a r r a y ) .   C a s e   o f   2 D   a r r a y   j u s t   n e e d s   o n e - m o r e   s t e p . 
 
 
 
 . .   i m a g e : :   A d v a n c e d O u t p u t A l p h a 1 . g i f 
 
       : c l a s s :   i n l i n e d - r i g h t 
 
 
 
 . .   c o d e - b l o c k : :   p y t h o n 
 
       : l i n e n o s : 
 
 
 
       i m p o r t   s y s ,   p y g a m e 
 
       p y g a m e . i n i t ( ) 
 
 
 
       s i z e   =   w i d t h ,   h e i g h t   =   3 2 0 ,   2 4 0 
 
       s p e e d   =   [ 2 ,   2 ] 
 
       b l a c k   =   0 ,   0 ,   0 
 
 
 
       s c r e e n   =   p y g a m e . d i s p l a y . s e t _ m o d e ( s i z e ) 
 
 
 
       b a l l   =   p y g a m e . i m a g e . l o a d ( " A d v a n c e d O u t p u t A l p h a 1 . g i f " ) 
 
       b a l l r e c t   =   b a l l . g e t _ r e c t ( ) 
 
 
 
       w h i l e   1 : 
 
               f o r   e v e n t   i n   p y g a m e . e v e n t . g e t ( ) : 
 
                       i f   e v e n t . t y p e   = =   p y g a m e . Q U I T :   s y s . e x i t ( ) 
 
 
 
               b a l l r e c t   =   b a l l r e c t . m o v e ( s p e e d ) 
 
               i f   b a l l r e c t . l e f t   <   0   o r   b a l l r e c t . r i g h t   >   w i d t h : 
 
                       s p e e d [ 0 ]   =   - s p e e d [ 0 ] 
 
               i f   b a l l r e c t . t o p   <   0   o r   b a l l r e c t . b o t t o m   >   h e i g h t : 
 
                       s p e e d [ 1 ]   =   - s p e e d [ 1 ] 
 
 
 
               s c r e e n . f i l l ( b l a c k ) 
 
               s c r e e n . b l i t ( b a l l ,   b a l l r e c t ) 
 
               p y g a m e . d i s p l a y . f l i p ( ) 
 
 
 
 . .   i m a g e : :   A d v a n c e d O u t p u t A l p h a 2 . g i f 
 
       : c l a s s :   i n l i n e d - r i g h t 
 
 
 
 . .   c o d e - b l o c k : :   p y t h o n 
 
       : l i n e n o s : 
 
 
 
       i m p o r t   s y s ,   p y g a m e 
 
       p y g a m e . i n i t ( ) 
 
 
 
       s i z e   =   w i d t h ,   h e i g h t   =   3 2 0 ,   2 4 0 
 
       s p e e d   =   [ 2 ,   2 ] 
 
       b l a c k   =   0 ,   0 ,   0 
 
 
 
       s c r e e n   =   p y g a m e . d i s p l a y . s e t _ m o d e ( s i z e ) 
 
 
 
       b a l l   =   p y g a m e . i m a g e . l o a d ( " A d v a n c e d O u t p u t A l p h a 2 . g i f " ) 
 
       b a l l r e c t   =   b a l l . g e t _ r e c t ( ) 
 
 
 
       w h i l e   1 : 
 
               f o r   e v e n t   i n   p y g a m e . e v e n t . g e t ( ) : 
 
                       i f   e v e n t . t y p e   = =   p y g a m e . Q U I T :   s y s . e x i t ( ) 
 
 
 
               b a l l r e c t   =   b a l l r e c t . m o v e ( s p e e d ) 
 
               i f   b a l l r e c t . l e f t   <   0   o r   b a l l r e c t . r i g h t   >   w i d t h : 
 
                       s p e e d [ 0 ]   =   - s p e e d [ 0 ] 
 
               i f   b a l l r e c t . t o p   <   0   o r   b a l l r e c t . b o t t o m   >   h e i g h t : 
 
                       s p e e d [ 1 ]   =   - s p e e d [ 1 ] 
 
 
 
               s c r e e n . f i l l ( b l a c k ) 
 
               s c r e e n . b l i t ( b a l l ,   b a l l r e c t ) 
 
               p y g a m e . d i s p l a y . f l i p ( ) 
 
 
 
 . .   i m a g e : :   A d v a n c e d O u t p u t A l p h a 3 . g i f 
 
       : c l a s s :   i n l i n e d - r i g h t 
 
 
 
 . .   c o d e - b l o c k : :   p y t h o n 
 
       : l i n e n o s : 
 
 
 
       i m p o r t   s y s ,   p y g a m e 
 
       p y g a m e . i n i t ( ) 
 
 
 
       s i z e   =   w i d t h ,   h e i g h t   =   3 2 0 ,   2 4 0 
 
       s p e e d   =   [ 2 ,   2 ] 
 
       b l a c k   =   0 ,   0 ,   0 
 
 
 
       s c r e e n   =   p y g a m e . d i s p l a y . s e t _ m o d e ( s i z e ) 
 
 
 
       b a l l   =   p y g a m e . i m a g e . l o a d ( " A d v a n c e d O u t p u t A l p h a 3 . g i f " ) 
 
       b a l l r e c t   =   b a l l . g e t _ r e c t ( ) 
 
 
 
       w h i l e   1 : 
 
               f o r   e v e n t   i n   p y g a m e . e v e n t . g e t ( ) : 
 
                       i f   e v e n t . t y p e   = =   p y g a m e . Q U I T :   s y s . e x i t ( ) 
 
 
 
               b a l l r e c t   =   b a l l r e c t . m o v e ( s p e e d ) 
 
               i f   b a l l r e c t . l e f t   <   0   o r   b a l l r e c t . r i g h t   >   w i d t h : 
 
                       s p e e d [ 0 ]   =   - s p e e d [ 0 ] 
 
               i f   b a l l r e c t . t o p   <   0   o r   b a l l r e c t . b o t t o m   >   h e i g h t : 
 
                       s p e e d [ 1 ]   =   - s p e e d [ 1 ] 
 
 
 
               s c r e e n . f i l l ( b l a c k ) 
 
               s c r e e n . b l i t ( b a l l ,   b a l l r e c t ) 
 
               p y g a m e . d i s p l a y . f l i p ( ) 
 
 
 
 g e n e r a t e b o a r d   f u n c t i o n   r e t u r n s   r a n d o m l y   g e n e r a t e d   2 D   b o a r d   w i t h   t h e   n u m b e r   o f   r e d   b l o c k   a n d   b l a c k   b l o c k .   N e e d l e s s   t o   e x p l a i n .   A l s o ,   p r i n t b o a r d   f u n c t i o n   p r i n t s   2 D   b o a r d   b y   s a m e   m e t h o d   a s   1 D   a r r a y .   O u t p u t   c o l o r   w i l l   b e   c h a n g e d   b y   d a t a   o f   b o a r d [ i ] [ j ]   i s   1   o r   n o t .   T h i s   b o a r d   i s   f o r   o u t p u t   o n l y .   P r o c e s s i n g   a b o u t   m a r g i n   s e e m s   t o   b e   a n n o y i n g   b e c a u s e   w e   h a v e   t o   k n o w   e x a c t   l o c a t i o n   b y   c a l c u l a t i n g .   R e m e m b e r   p r o l o g .   O u t p u t   ( e x e c u t i n g   r e s u l t )   o f   P y g a m e   i s   G U I   b u t   i n p u t ( c o d i n g )   o f   P y g a m e   i s   C U I .   T h a t   i s   P y g a m e . 
 
 
 
 A c t u a l l y ,   t h e r e   a r e   a   l o t   o f   i d e a   f o r   i m p r o v i n g   t h i s   g a m e .   H o w   a b o u t   c h a n g i n g   b u t t o n   i n t o   i m a g e   f i l e ?   H o w   a b o u t   a d d i n g   s o u n d   e f f e c t   w h e n   p r e v i o u s   c h o s e   w a s   c o r r e c t   o r   n o t ?   H o w   a b o u t   s e t t i n g   t i m e   l i m i t ?   H o w   a b o u t   a d d i n g   v i s u a l   e f f e c t   w h e n   p l a y e r   w i n ( m a x H P )   o r   n o t ( m i n H P ) ?   H o w   a b o u t   m a k e   t h e   b o a r d   b i g g e r   w i t h   a n o t h e r   c o l o r s ?   H o w   a b o u t   i m p l e m e n t   F l o o d - i t   g a m e   b y   g i v e n   i n t e r f a c e ?   T h e r e   a r e   s t i l l   l o t s   o f   s e l e c t i o n   b e c a u s e   t h i s   g a m e   i s   s i m p l e . 
 
 
 
 < R e f e r e n c e   C o d e >   : : 
 
 
 
         i m p o r t   p y g a m e ,   s y s ,   r a n d o m 
 
         f r o m   p y g a m e . l o c a l s   i m p o r t * 
 
         
 
         m a x H P   =   1 0   
 
         w h i t e   =   ( 2 5 5 , 2 5 5 , 2 5 5 ) 
 
         g r a y   =   ( 1 2 7 , 1 2 7 , 1 2 7 ) 
 
         b l a c k   =   ( 0 , 0 , 0 ) 
 
         r e d   =   ( 2 5 5 , 0 , 0 ) 
 
         g r e e n   =   ( 0 , 2 5 5 , 0 ) 
 
         b l u e   =   ( 0 , 0 , 2 5 5 ) 
 
         p y g a m e . i n i t ( ) 
 
         p y g a m e . d i s p l a y . s e t _ c a p t i o n ( " R e d   o r   B l a c k   P r o j e c t " ) 
 
         w i d t h   =   6 4 0   
 
         h e i g h t   =   4 8 0 
 
         m y S c r e e n   =   p y g a m e . d i s p l a y . s e t _ m o d e ( ( w i d t h ,   h e i g h t ) ) 
 
         m y T e x t F o n t   =   p y g a m e . f o n t . F o n t ( " H o o n W h i t e c a t R . t t f " ,   3 2 ) 
 
         m y T e x t   =   m y T e x t F o n t . r e n d e r ( ( s t r ( m a x H P )   +   " / "   +   s t r ( m a x H P ) ) ,   T r u e ,   r e d ,   g r a y ) 
 
         m y T e x t A r e a   =   m y T e x t . g e t _ r e c t ( ) 
 
         m y T e x t A r e a . c e n t e r   =   ( w i d t h / 2 ,   h e i g h t / 2 ) 
 
         f p s C l o c k   =   p y g a m e . t i m e . C l o c k ( ) 
 
         
 
         d e f   m a i n ( ) : 
 
                 H P   =   5 
 
                 b o a r d ,   b _ r e d ,   b _ b l a c k   =   g e n e r a t e B o a r d ( 5 , 5 )   # 1 
 
                 
 
                 w h i l e   T r u e : 
 
                         m y T e x t   =   m y T e x t F o n t . r e n d e r ( ( s t r ( H P )   +   " / "   +   s t r ( m a x H P ) ) ,   T r u e ,   r e d ,   g r a y ) 
 
                 
 
                         m y S c r e e n . f i l l ( g r a y ) 
 
         
 
                         m y S c r e e n . b l i t ( m y T e x t ,   m y T e x t A r e a ) 
 
                         d r a w H P ( H P ) 
 
                         d r a w B u t t o n s ( ) 
 
                         d r a w B o a r d ( b o a r d )   # 2 
 
         
 
                         f o r   e v e n t   i n   p y g a m e . e v e n t . g e t ( ) : 
 
                                 i f   e v e n t . t y p e   = =   Q U I T : 
 
                                         p y g a m e . q u i t ( ) 
 
                                         s y s . e x i t ( ) 
 
                                         
 
                                 e l i f   e v e n t . t y p e   = =   K E Y D O W N : 
 
                                         i f   e v e n t . k e y   = =   K _ U P : 
 
                                                 i f   H P   ! =   1 0 : 
 
                                                         H P   =   H P   +   1 
 
                                         e l i f   e v e n t . k e y   = =   K _ D O W N : 
 
                                                 i f   H P   ! =   0 : 
 
                                                         H P   =   H P   -   1 
 
                                 e l i f   e v e n t . t y p e   = =   M O U S E B U T T O N U P : 
 
                                         x ,   y   =   e v e n t . p o s 
 
                                         
 
                                         i f   p y g a m e . R e c t ( 2 7 0 ,   4 2 5 ,   4 5 ,   4 5 ) . c o l l i d e p o i n t ( x ,   y ) :   # 3 
 
                                                 i f   b _ r e d   > =   b _ b l a c k : 
 
                                                         i f   H P   ! =   1 0 : 
 
                                                                 H P   =   H P   +   1 
 
                                                         b o a r d ,   b _ r e d ,   b _ b l a c k   =   g e n e r a t e B o a r d ( 5 , 5 ) 
 
                                                 e l i f   b _ r e d   <   b _ b l a c k : 
 
                                                         i f   H P   ! =   0 : 
 
                                                                 H P   =   H P   -   1 
 
                                                         b o a r d ,   b _ r e d ,   b _ b l a c k   =   g e n e r a t e B o a r d ( 5 , 5 ) 
 
                                                         
 
                                         e l i f   p y g a m e . R e c t ( 3 2 5 ,   4 2 5 ,   4 5 ,   4 5 ) . c o l l i d e p o i n t ( x ,   y ) :   # 4 
 
                                                 i f   b _ r e d   < =   b _ b l a c k : 
 
                                                         i f   H P   ! =   1 0 : 
 
                                                                 H P   =   H P   +   1 
 
                                                         b o a r d ,   b _ r e d ,   b _ b l a c k   =   g e n e r a t e B o a r d ( 5 , 5 ) 
 
                                                 e l i f   b _ r e d   >   b _ b l a c k : 
 
                                                         i f   H P   ! =   0 : 
 
                                                                 H P   =   H P   -   1 
 
                                                         b o a r d ,   b _ r e d ,   b _ b l a c k   =   g e n e r a t e B o a r d ( 5 , 5 ) 
 
                 
 
                         p y g a m e . d i s p l a y . u p d a t e ( ) 
 
                         f p s C l o c k . t i c k ( 6 0 ) 
 
         
 
         d e f   d r a w H P ( H P ) : 
 
                 r   =   i n t ( ( h e i g h t   -   4 0 )   /   m a x H P ) 
 
         
 
                 p y g a m e . d r a w . r e c t ( m y S c r e e n ,   g r a y ,   ( 2 0 ,   2 0 ,   2 0 ,   2 0   +   ( ( m a x H P   -   0 . 5 )   *   r ) ) ) 
 
         
 
                 f o r   i   i n   r a n g e ( m a x H P ) : 
 
                         i f   H P   > =   ( m a x H P   -   i ) : 
 
                                 p y g a m e . d r a w . r e c t ( m y S c r e e n ,   b l u e ,   ( 2 0 ,   2 0   +   ( i   *   r ) ,   2 0 ,   r ) ) 
 
                         p y g a m e . d r a w . r e c t ( m y S c r e e n ,   w h i t e ,   ( 2 0 ,   2 0   +   ( i   *   r ) ,   2 0 ,   r ) ,   1 ) 
 
         
 
                 r e t u r n 
 
         
 
         d e f   d r a w B u t t o n s ( ) : 
 
                 r   =   4 5 
 
                 r _ m a r g i n   =   1 0 
 
                 c o l o r s   =   [ r e d ,   b l a c k ] 
 
                 
 
                 n u m   =   2 
 
                 m a r g i n   =   i n t ( ( w i d t h   -   ( ( r   *   n u m )   +   ( r _ m a r g i n   *   ( n u m   -   1 ) ) ) )   /   2 ) 
 
                 
 
                 f o r   i   i n   r a n g e ( 0 ,   n u m ) : 
 
                         l e f t   =   m a r g i n   +   ( i   *   r )   +   ( i   *   r _ m a r g i n ) 
 
                         u p   =   h e i g h t   -   r   -   1 0 
 
                         p y g a m e . d r a w . r e c t ( m y S c r e e n ,   c o l o r s [ i ] ,   ( l e f t ,   u p ,   r ,   r ) ) 
 
                         p y g a m e . d r a w . r e c t ( m y S c r e e n ,   g r a y ,   ( l e f t   +   2 ,   u p   +   2 ,   r   -   4 ,   r   -   4 ) ,   2 )         
 
         
 
         d e f   g e n e r a t e B o a r d ( w i d t h ,   h e i g h t ) :   # 5 
 
                 b o a r d   =   [ ] 
 
                 b _ r e d   =   0 
 
                 b _ b l a c k   =   0 
 
                 
 
                 f o r   x   i n   r a n g e ( w i d t h ) : 
 
                         c o l u m n   =   [ ] 
 
                         f o r   y   i n   r a n g e ( h e i g h t ) : 
 
                                 c o l u m n . a p p e n d ( r a n d o m . r a n d i n t ( 0 ,   1 ) ) 
 
                         b o a r d . a p p e n d ( c o l u m n ) 
 
         
 
                 f o r   x   i n   r a n g e ( w i d t h ) : 
 
                         f o r   y   i n   r a n g e ( h e i g h t ) : 
 
                                 i f ( b o a r d [ x ] [ y ]   = =   1 ) : 
 
                                           b _ r e d   =   b _ r e d   +   1 
 
                                 e l i f ( b o a r d [ x ] [ y ]   = =   0 ) : 
 
                                         b _ b l a c k   =   b _ b l a c k   +   1 
 
                         
 
                 r e t u r n   b o a r d ,   b _ r e d ,   b _ b l a c k 
 
         
 
         d e f   d r a w B o a r d ( b o a r d ) :   # 6 
 
                 r   =   5 0 
 
                 b _ w i d t h   =   5 
 
                 b _ h e i g h t   =   5 
 
                 l _ m a r g i n   =   i n t ( ( w i d t h   -   ( b _ w i d t h   *   r ) )   /   2 ) 
 
                 u _ m a r g i n   =   i n t ( ( h e i g h t   -   ( b _ h e i g h t   *   r ) )   /   2 ) 
 
         
 
                 f o r   x   i n   r a n g e ( 5 ) : 
 
                         f o r   y   i n   r a n g e ( 5 ) : 
 
                                 l e f t   =   x   *   r   +   l _ m a r g i n 
 
                                 u p   =   y   *   r   +   u _ m a r g i n 
 
                                 i f   b o a r d [ x ] [ y ]   = =   1 : 
 
                                         c o l o r   =   r e d ; 
 
                                 e l i f   b o a r d [ x ] [ y ]   = =   0 : 
 
                                         c o l o r   =   b l a c k 
 
                                 p y g a m e . d r a w . r e c t ( m y S c r e e n ,   c o l o r ,   ( l e f t ,   u p ,   r ,   r ) ) 
 
                                 
 
                 l e f t   =   l _ m a r g i n 
 
                 u p   =   u _ m a r g i n 
 
                 p y g a m e . d r a w . r e c t ( m y S c r e e n ,   w h i t e ,   ( l e f t - 1 ,   u p - 1 ,   r   *   5   +   1 ,   r   *   b _ h e i g h t   +   1 ) ,   1 ) 
 
         
 
         i f   _ _ n a m e _ _   = =   ' _ _ m a i n _ _ ' : 
 
                 m a i n ( ) 
 
         